package top.ljlgithub.api.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.ljlgithub.api.bean.Attr;
import top.ljlgithub.api.bean.AttrParam;
import top.ljlgithub.api.bean.SelParam;
import top.ljlgithub.api.common.response.Meta;
import top.ljlgithub.api.common.response.ResponseEntity;
import top.ljlgithub.api.exception.ParamException;
import top.ljlgithub.api.service.AttrService;

import java.util.List;

/**
 * @ClassName
 * @Description TODO
 * @Author L1470
 * @Date 2021/5/7 9:40
 * @Version 1.0
 **/
@CrossOrigin
@RestController
public class AttrController {
    @Autowired
    AttrService attrService;

    @RequiresPermissions("user:getAttributes:*")
    @GetMapping("/categories/{id}/attributes")
    public ResponseEntity getAttrList(@PathVariable("id") Integer id, SelParam selParam) throws ParamException {
        String sel = selParam.getSel();
        if (sel == null){
            throw new ParamException();
        }
        Meta meta = new Meta("获取成功",200);
        List<Attr> attrList = attrService.getAttrList(id, sel);
        ResponseEntity<Object> result = new ResponseEntity<>(attrList,meta);
        return result;
    }

    @RequiresPermissions("user:createAttribute:*")
    @PostMapping("/categories/{id}/attributes")
    public ResponseEntity addAttr(@PathVariable("id") Integer id,
                                  @RequestBody AttrParam attrParam) throws ParamException {
        String attr_name = attrParam.getAttr_name();
        String attr_sel = attrParam.getAttr_sel();
        if (attr_name == null || attr_sel == null){
            throw new ParamException();
        }
        String attr_vals = attrParam.getAttr_vals();
        Attr attr = new Attr();
        attr.setCat_id(id);
        attr.setAttr_name(attr_name);
        attr.setAttr_sel(attr_sel);
        attr.setAttr_vals(attr_vals);
        attr = attrService.addAttr(attr);
        Meta meta = new Meta("创建成功",201);
        ResponseEntity<Object> result = new ResponseEntity<>(attr,meta);
        return result;
    }

    @RequiresPermissions("user:deleteAttribute:*")
    @DeleteMapping("/categories/{id}/attributes/{attrid}")
    public ResponseEntity getAttrList(@PathVariable("id") Integer id, @PathVariable("attrid") Integer attrid){
        Meta meta = new Meta("删除成功",200);
        attrService.deleteAttr(id, attrid);
        ResponseEntity<Object> result = new ResponseEntity<>(null,meta);
        return result;
    }

    @RequiresPermissions("user:getCategoryById:*")
    @GetMapping("/categories/{id}/attributes/{attrid}")
    public ResponseEntity findAttrById(@PathVariable("id") Integer id, @PathVariable("attrid") Integer attrid){
        Meta meta = new Meta("获取成功",200);
        Attr attr = attrService.findAttrById(id, attrid);
        ResponseEntity<Object> result = new ResponseEntity<>(attr,meta);
        return result;
    }

    @PutMapping("/categories/{id}/attributes/{attrid}")
    public ResponseEntity updateAttr(@PathVariable("id") Integer id,
                                  @PathVariable("attrid") Integer attrid,
                                  @RequestBody AttrParam attrParam) throws ParamException {
        String attr_name = attrParam.getAttr_name();
        String attr_sel = attrParam.getAttr_sel();
        if (attr_name == null || attr_sel == null){
            throw new ParamException();
        }
        String attr_vals = attrParam.getAttr_vals();
        Attr attr = new Attr();
        attr.setCat_id(id);
        attr.setAttr_id(attrid);
        attr.setAttr_name(attr_name);
        attr.setAttr_sel(attr_sel);
        attr.setAttr_vals(attr_vals);
        Meta meta = new Meta("更新成功",200);
        attr = attrService.updateAttr(attr);
        ResponseEntity<Object> result = new ResponseEntity<>(attr,meta);
        return result;
    }
}
